﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using iWare.Wms.Core;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;

namespace iWare.Wms.Application
{
    /// <summary>
    /// 库位视图
    /// </summary>
    [ApiDescriptionSettings("统计查询", Name = "LocationView", Order = 100)]
    [Route("api/[Controller]")]
    public class LocationViewService : ILocationViewService, IDynamicApiController, ITransient
    {
        private readonly IRepository<WmsContainer, MasterDbContextLocator> _wmsContainerRep;
        private readonly IRepository<WmsMaterial, MasterDbContextLocator> _wmsMaterialRep;
        private readonly IRepository<WmsMaterialContainer, MasterDbContextLocator> _wmsMaterialContainerRep;
        private readonly IRepository<WmsArea, MasterDbContextLocator> _wmsAreaRep;
        private readonly IRepository<WmsPlace, MasterDbContextLocator> _wmsPlaceRep;
        private readonly IRepository<WmsTask, MasterDbContextLocator> _wmsTaskRep;
        private readonly IRepository<WmsMaterialStock, MasterDbContextLocator> _wmsMaterialStockRep;
        private readonly IRepository<WmsContainerPlace, MasterDbContextLocator> _wmsContainerPlaceRep;


        public LocationViewService(
             IRepository<WmsContainer, MasterDbContextLocator> wmsContainerRep,
            IRepository<WmsMaterial, MasterDbContextLocator> wmsMaterialRep,
            IRepository<WmsMaterialContainer, MasterDbContextLocator> wmsMaterialContainerRep,
            IRepository<WmsArea, MasterDbContextLocator> wmsAreaRep,
            IRepository<WmsPlace, MasterDbContextLocator> wmsPlaceRep,
            IRepository<WmsTask, MasterDbContextLocator> wmsTaskRep,
            IRepository<WmsMaterialStock, MasterDbContextLocator> wmsMaterialStockRep,
            IRepository<WmsContainerPlace, MasterDbContextLocator> wmsContainerPlaceRep
        )
        {
            _wmsContainerRep = wmsContainerRep;
            _wmsMaterialRep = wmsMaterialRep;
            _wmsMaterialContainerRep = wmsMaterialContainerRep;
            _wmsAreaRep = wmsAreaRep;
            _wmsPlaceRep = wmsPlaceRep;
            _wmsTaskRep = wmsTaskRep;
            _wmsMaterialStockRep = wmsMaterialStockRep;
            _wmsContainerPlaceRep = wmsContainerPlaceRep;
        }

        /// <summary>
        /// 获取库区信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("GetArea")]
        public async Task<List<WmsArea>> GetArea()
        {
            var areaList = await _wmsAreaRep.Where(n => n.AreaStatus == CommonStatus.ENABLE).ToListAsync();
            return areaList;
        }

        /// <summary>
        /// 根据库区获取排
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("GetPalceRowno")]
        public async Task<object> GetPalceRowno([FromQuery] GetPalceRownoInput input)
        {
            return await _wmsPlaceRep.Where(n => n.AreaId == input.Areaid).Select(n => n.RowNo).Distinct().ToListAsync();
        }


        /// <summary>
        /// 根据库区 排获取库位信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("GetPalceList")]
        public async Task<PalceInfoOtput> GetPalceList([FromQuery] GetPalceListInput input)
        {
            var palceList = await _wmsPlaceRep.Where(p => p.AreaId == input.Areaid && p.RowNo == input.Rowno).ToListAsync();
            return new PalceInfoOtput()
            {
                countNum = palceList.Count,
                cunhuoNum = palceList.Where(n => n.PlaceStatus == Core.Enum.PlaceStatus.CUNHUO).Count(),
                emptyNum = palceList.Where(n => n.PlaceStatus == Core.Enum.PlaceStatus.KONGXIAN).Count(),
                emptyContainerNum = palceList.Where(p => p.EmptyContainer == YesOrNot.Y && p.PlaceStatus == Core.Enum.PlaceStatus.CUNHUO).Count(),
                materialNum = palceList.Where(p => p.EmptyContainer == YesOrNot.N && p.PlaceStatus == Core.Enum.PlaceStatus.CUNHUO).Count(),
                lockNum = palceList.Where(p => p.Islock == YesOrNot.Y).Count(),
                PalceDetails = palceList.Adapt<List<PalceDetail>>()
            };
        }

        /// <summary>
        /// 根据库位获取对应信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("GetMaterialDetail")]
        [UnifyResult(typeof(MaterialDetailsOutput))]
        public async Task<object> GetMaterialDetail([FromQuery] GetMaterialDetailInput input)
        {
            var palceModel = await _wmsPlaceRep.FirstOrDefaultAsync(p => p.Id == input.ID);
            if (palceModel == null) return XnRestfulResultProvider.RESTfulMesaage("库位信息不存在!");
            var MaterialStockList = await _wmsMaterialStockRep.Where(p => p.PlaceCode == palceModel.PlaceCode).ToListAsync();
            var model = new MaterialDetailsOutput()
            {
                PalceDetails = palceModel.Adapt<PalceDetail>()
            };
            if (MaterialStockList.Count>0) {
                model.Containercode = MaterialStockList.FirstOrDefault().ContainerCode;
                model.WmsMaterialStocks = MaterialStockList;
            }
            return XnRestfulResultProvider.RESTfulResult(model);
        }

        /// <summary>
        /// 修改库位锁定信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("UpdatePalceIslock")]
        [UnifyResult(typeof(object))]
        public async Task<object> UpdatePalceIslock([FromQuery] GetMaterialDetailInput input)
        {
            var palceModel = await _wmsPlaceRep.FirstOrDefaultAsync(p => p.Id == input.ID);
            if (palceModel == null) return XnRestfulResultProvider.RESTfulMesaage("库位信息不存在!");
            if (palceModel.Islock == YesOrNot.Y)
            {
                palceModel.Islock = YesOrNot.N;
            }
            else
            {
                palceModel.Islock = YesOrNot.Y;
            }
            await _wmsPlaceRep.UpdateAsync(palceModel);
            return XnRestfulResultProvider.RESTfulResult();
        }
    }
}
